package com.beirtipol.travel.sax;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

public class SAXFactory
{
	private final SAXParserFactory	factory	= SAXParserFactory.newInstance();

	public void parse(FeedHandler handler)
	{
		InputStream in = null;
		try
		{
			URL url = new URL(handler.getURL());
			URLConnection conn = url.openConnection();

			SAXParser parser = factory.newSAXParser();
			in = conn.getInputStream();
			parser.parse(in, handler);
		}
		catch (ParserConfigurationException e)
		{
			e.printStackTrace();
		}
		catch (SAXException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (in != null)
			{
				try
				{
					in.close();
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
			}
		}
	}
}
